package examples.springcloud.controller;

import cn.hutool.json.JSONUtil;
import examples.springcloud.common.PageVo;
import examples.springcloud.common.Result;
import examples.springcloud.dto.NodeData;
import examples.springcloud.dto.NodeTableData;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.springcloud.annotation.ShenyuGetMapping;
import org.apache.shenyu.client.springcloud.annotation.ShenyuPostMapping;
import org.apache.shenyu.client.springcloud.annotation.ShenyuSpringCloudClient;
import examples.springcloud.dto.UserDTO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * TestController.
 */
@RestController
@RequestMapping("/test1")
@ShenyuSpringCloudClient(path = "/test1")
@ApiModule("/test1")
public class Test1Controller {


    /**
     * Post string.
     *
     * @param userDTO the user dto
     * @return the string
     */
    @ShenyuPostMapping("/save")
    @ApiDoc(desc = "/save")
    public UserDTO post(@RequestBody final UserDTO userDTO) {
        userDTO.setUserName("hello world spring cloud save user");
        return userDTO;
    }

    /**
     * Find by user id user dto.
     *
     * @param userId the user id
     * @return the user dto
     */
    @ShenyuGetMapping("/findByUserId")
    @ApiDoc(desc = "/findByUserId")
    public UserDTO findByUserId(@RequestParam("userId") final String userId) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        userDTO.setUserName("hello world spring cloud findBy user");
        return userDTO;
    }

    /**
     * mock接口1
     */
    @ShenyuGetMapping("/nodeSynData")
    @ApiDoc(desc = "/nodeSynData")
    public Result nodeSynData() {
        String json = "[\n" +
                "        {\n" +
                "            \"name\": \"9月22日\",\n" +
                "            \"value\": 100\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"9月23日\",\n" +
                "            \"value\": 140\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"9月24日\",\n" +
                "            \"value\": 240\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"9月25日\",\n" +
                "            \"value\": 100\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"9月26日\",\n" +
                "            \"value\": 100\n" +
                "        }\n" +
                "    ]";
        List<NodeData> list = JSONUtil.toList(json, NodeData.class);
        return Result.success(list);
    }

    /**
     * mock接口2
     */
    @ShenyuGetMapping("/nodeStorage")
    @ApiDoc(desc = "/nodeStorage")
    public Result nodeStorage() {
        String json = "[\n" +
                "        {\n" +
                "            \"name\": \"已占用\",\n" +
                "            \"value\": 300\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"未占用\",\n" +
                "            \"value\": 500\n" +
                "        }\n" +
                "    ]";
        List<NodeData> list = JSONUtil.toList(json, NodeData.class);
        return Result.success(list);
    }

    /**
     * mock接口3
     */
    @ShenyuGetMapping("/nodeStatus")
    @ApiDoc(desc = "/nodeStatus")
    public Result nodeStatus() {
        String json = " [\n" +
                "        {\n" +
                "            \"name\": \"使用\",\n" +
                "            \"value\": 32\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"空闲\",\n" +
                "            \"value\": 44\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"关机\",\n" +
                "            \"value\": 10\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"故障\",\n" +
                "            \"value\": 2\n" +
                "        }\n" +
                "    ]";
        List<NodeData> list = JSONUtil.toList(json, NodeData.class);
        return Result.success(list);
    }

    /**
     * mock接口4
     */
    @ShenyuGetMapping("/getNodeTableData")
    @ApiDoc(desc = "/getNodeTableData")
    public Result getNodeTableData(@RequestParam int pageNum, @RequestParam int pageSize) {
        String json = " [\n" +
                "            {\n" +
                "                \"id\": \"0\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-0-60\",\n" +
                "                \"totalStorage\": 45,\n" +
                "                \"usedStorage\": 496,\n" +
                "                \"synthesisStatus\": \"已完成\",\n" +
                "                \"nodeStatus\": \"负载\",\n" +
                "                \"nodeTaskProgress\": \"1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"1\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-1-40\",\n" +
                "                \"totalStorage\": 785,\n" +
                "                \"usedStorage\": 239,\n" +
                "                \"synthesisStatus\": \"未完成\",\n" +
                "                \"nodeStatus\": \"空闲\",\n" +
                "                \"nodeTaskProgress\": \"0.92\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"2\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-2-63\",\n" +
                "                \"totalStorage\": 989,\n" +
                "                \"usedStorage\": 241,\n" +
                "                \"synthesisStatus\": \"已完成\",\n" +
                "                \"nodeStatus\": \"负载\",\n" +
                "                \"nodeTaskProgress\": \"1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"3\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-3-66\",\n" +
                "                \"totalStorage\": 529,\n" +
                "                \"usedStorage\": 192,\n" +
                "                \"synthesisStatus\": \"未完成\",\n" +
                "                \"nodeStatus\": \"空闲\",\n" +
                "                \"nodeTaskProgress\": \"0.28\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"4\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-4-80\",\n" +
                "                \"totalStorage\": 354,\n" +
                "                \"usedStorage\": 172,\n" +
                "                \"synthesisStatus\": \"已完成\",\n" +
                "                \"nodeStatus\": \"负载\",\n" +
                "                \"nodeTaskProgress\": \"1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"5\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-5-90\",\n" +
                "                \"totalStorage\": 28,\n" +
                "                \"usedStorage\": 426,\n" +
                "                \"synthesisStatus\": \"未完成\",\n" +
                "                \"nodeStatus\": \"空闲\",\n" +
                "                \"nodeTaskProgress\": \"0.32\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"6\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-6-52\",\n" +
                "                \"totalStorage\": 118,\n" +
                "                \"usedStorage\": 827,\n" +
                "                \"synthesisStatus\": \"已完成\",\n" +
                "                \"nodeStatus\": \"负载\",\n" +
                "                \"nodeTaskProgress\": \"1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"7\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-7-74\",\n" +
                "                \"totalStorage\": 768,\n" +
                "                \"usedStorage\": 978,\n" +
                "                \"synthesisStatus\": \"未完成\",\n" +
                "                \"nodeStatus\": \"空闲\",\n" +
                "                \"nodeTaskProgress\": \"0.06\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"8\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-8-88\",\n" +
                "                \"totalStorage\": 192,\n" +
                "                \"usedStorage\": 950,\n" +
                "                \"synthesisStatus\": \"已完成\",\n" +
                "                \"nodeStatus\": \"负载\",\n" +
                "                \"nodeTaskProgress\": \"1\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": \"9\",\n" +
                "                \"nodeImgUrl\": \"https://img.i2soft.net/i2official-web/assets/images/nav/navLogo.png\",\n" +
                "                \"nodeInfo\": \"节点-9-38\",\n" +
                "                \"totalStorage\": 575,\n" +
                "                \"usedStorage\": 990,\n" +
                "                \"synthesisStatus\": \"未完成\",\n" +
                "                \"nodeStatus\": \"空闲\",\n" +
                "                \"nodeTaskProgress\": \"0.35\"\n" +
                "            }\n" +
                "        ]";
        List<NodeTableData> list = JSONUtil.toList(json, NodeTableData.class);
        PageVo pageVo = new PageVo();
        pageVo.setRecords(list);
        pageVo.setTotal(list.size());
        pageVo.setPageNum(pageNum);
        pageVo.setPageSize(pageSize);
        return Result.success(pageVo);
    }
}
